import { CSSProperties } from 'react';
import {
    getSystemInfoSync,
    pxTransform as taro_pxTransform,
    canIUse,
    getWindowInfo as taro_getWindowInfo
} from '@tarojs/taro';
export const setStyleSize = function (width?: string | number, height?: string | number) {
    const styleObj: Record<string, number | string> = {};
    if (width) {
        styleObj.width = typeof width === 'number' ? taro_pxTransform(Number(width)) : width;
    }
    if (height) {
        styleObj.height = typeof height === 'number' ? taro_pxTransform(Number(height)) : height;
    }
    return styleObj as CSSProperties;
};

export const pxTransform = function (value: number) {
    return taro_pxTransform(value);
};

export const toPX = (value: number) => {
    const clientWidth = getSystemInfoSync().windowWidth;
    return (value * clientWidth) / 750;
};
export const getWindowInfo = (): Taro.getWindowInfo.Result => {
    return canIUse?.('getWindowInfo') ? taro_getWindowInfo() : getSystemInfoSync();
};
export const getSafeSize = function () {
    const winInfo = getWindowInfo();
    if (winInfo.safeArea?.bottom) {
        return winInfo.screenHeight - winInfo.safeArea.bottom;
    }
    return 0;
};
export const pxCheck = (value: string | number): string => {
    return Number.isNaN(Number(value)) ? String(value) : `${value}px`;
};
